import { fileURLToPath, URL } from "node:url"

import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"
import legacy from "@vitejs/plugin-legacy"
import AutoImport from "unplugin-auto-import/vite"
import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import ElementPlus from "unplugin-element-plus/vite"

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    legacy({
      targets: ["defaults", "not IE 11"],
    }),
    AutoImport({
      include: [".ts", ".js", ".vue"],
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      dirs: [],
      resolvers: [ElementPlusResolver()],
    }),
    ElementPlus(),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: '@use "@/styles/common.scss" as *;',
      },
    },
  },
  preview: {
    port: 9099,
  },
  server: {
    host: true,
    port: 6001,
    watch: {
      usePolling: true, // 修复HMR热更新失效
    },
    cors: true,
    proxy: {
      "/api": {
        target: "http://192.168.1.107:20007",
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/api/, '')
      },
    },
  },
  build: {
    minify: "terser",
    assetsDir: "static/assets/",
    rollupOptions: {
      output: {
        // manualChunks(id) {
        //   if (id.includes('node_modules')) {
        //     return id.toString().split('node_modules/')[1].split('/')[0].toString()
        //   }
        // },
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
        assetFileNames: "static/[ext]/[name]-[hash].[ext]",
        // manualChunks: {
        //   // vue vue-router合并打包
        //   vue: ['vue', 'vue-router'],
        //   echarts: ['echarts'],
        //   element: ['element-plus'],
        //   // 两个文件合并成一个helloWorld文件
        //   // helloWorld: ['src/components/HelloWorld.vue', 'src/components/HelloWorld1.vue'],
        // },
        // manualChunks: {
        //   // vue vue-router合并打包
        //   vue: ['vue',],
        //   vuerouter: ['vue-router'],
        //   echarts: ['echarts'],
        //   element: ['element-plus'],
        //   swiper: ['swiper'],
        //   axios: ['axios'],
        //   bignumber: ['bignumber.js'],
        //   lottieweb: ['lottie-web'],
        //   xgplayer: ['xgplayer'],
        //   vueusecore: ['@vueuse/core'],
        //   // 两个文件合并成一个helloWorld文件
        //   // helloWorld: ['src/components/HelloWorld.vue', 'src/components/HelloWorld1.vue'],
        // }
      },
    },
    chunkSizeWarningLimit: 512,
    terserOptions: {
      compress: {
        //生产环境时移除console
        drop_console: true,
        drop_debugger: true,
      },
    },
  },
})
